/**
 * Created by Baggio on 2017-11-22.
 */
$(function () {
    $("#pay").click(function () {
        var orderId = $("#orderId").val();
        var price = $("#price").val();
        /*3：网站支付宝*/

        var payMode = $('input:radio[name="payMode"]:checked').val();
        var consigneeName = $('input:text[name="consigneeName"]').val();
        var consigneePhone = $('input:text[name="consigneePhone"]').val();
        var consigneeAddress = $('input:text[name="consigneeAddress"]').val();
        var tid = $('input:hidden[name="tid"]').val();
        var postData = {
            orderId: orderId,
            orderPrice: price,
            price: price,
            payType: 1,
            payMode: payMode,
            status: 1,
            "consigneeInfo.consigneeAddress": consigneeAddress,
            "consigneeInfo.consigneePhone": consigneePhone,
            "consigneeInfo.consigneeName": consigneeName,
           "consigneeInfo.tid": tid
        };

        $.ajax({
            type: "post",
            url: "/pay/toPay.html",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                console.log(postData)
                console.log(res);
                if (res.code != 1) {
                    alert(res.msg);
                    return;
                }
                if (payMode == 4) {
                    toWeChatPay(res.data);

                }
                if (payMode == 3) {
                    toAiPay(res.data);
                }

            }

        });


    });


    $(".box_QR .fork_esc").click(function(){
        $(this).parents(".box_QR").hide();
        $(this).parents(".box_QR").siblings(".occlusion_box").hide();

    });
    $(".occlusion_box").click(function(){
        $(this).hide();
        $(this).siblings(".box_QR").hide();
    })
});
function toWeChatPay(payParameter) {
    console.log(payParameter);

    $(".occlusion_box").show();
    $(".box_QR").show();

    var path = "/pay/getTwoDimensionCode.html?codeUrl=" + payParameter.map.codeUrl;
    $("#weChatPay").find("img").attr("src", path);
    weChatPayCheck(payParameter.map.orderId);

}
function weChatPayCheck(orderId) {
    // 300000毫秒 = 5分钟，如果5分钟
    // 还不支付则跳转超时
    var timeTask;
    var allowTime = 0;

    timeTask = setInterval(function () {
        if (allowTime < 300000) {
            console.log(orderId);
            allowTime += 10000;
            if (orderId == '' || orderId == undefined) {
                clearInterval(timeTask);
            }
            var url = "/pay/payResult.html?ordrId=" + orderId;
            $.ajax({
                type: "post",
                dataType: "json",
                data: {orderId: orderId},
                url: "/pay/queryWxOrder.html",
                success: function (result) {
                    console.log(result);

                    if (result.data.resultCode == 1) {
                        location.href = url;

                    }
                    else if (result.data.resultCode == 0) {
                        /**
                         * 继续轮询
                         */
                    }
                    else {
                        location.href = url
                            + "&errorStr=" + result.data.resultMsg + "&errorCode=" + result.data.resultCode;

                    }

                },
                error: function (e) {
                    console.log(e.msg);
                }
            });
        } else {
            clearInterval(timeTask);
            location.href = location.href = url
                + "&errorStr=支付超时&errorCode=" + result.data.resultCode;
        }
    }, 10000);
}
function toAiPay(payParameter) {
    console.log(payParameter);
    var bankUrl = payParameter.bankUrl;
    var form = $("<form method='get' accept-charset='utf-8'></form>")
    form.attr({"action": bankUrl});
    var map = payParameter.map;
    for (var key in map) {//遍历json对象的每个key/value对,p为key
        var value = map[key];
        console.log(key + "<====>" + value);
        var input = $("<input type='hidden'>").attr({name: key, value: value});
        form.append(input);
    }
    console.log(form.html());
    $(document.body).append(form);   //使用JQuery将上述表单加入到body中
    form.submit();

}

